package com.blog.cloud.service.bac.grab;

import java.io.File;
import java.util.List;

import com.blog.cloud.comm.modal.pojo.GalleryTags;
import com.blog.cloud.comm.modal.pojo.Tags;
import com.blog.cloud.comm.modal.pojo.UserUploadInf;
import com.blog.cloud.comm.webutils.DateUtils;
import com.blog.cloud.comm.webutils.GetWaterId;
import com.blog.cloud.dao.GalleryTagsDao;
import com.blog.cloud.dao.TagsDao;
import com.blog.cloud.dao.UserUploadInfDao;

public class GrabService {
	
	public static GrabService ser = new GrabService();
	
	
	
	public void initGalleryData(){
		String path = "D:\\Manipula\\Manipula\\pic\\p-clazz";
		this.readFile(path);
	}
	
	
	
	
	String tagNo1 = "";
	String tagName = "";
	
	List<Tags> tags = null;
	
	public void readFile(String path){
//		this.inintTags();
		tags = TagsDao.me.geetTags();
		
		
		File file = new File(path);
		File[] listFiles = file.listFiles();
		for (File file2 : listFiles) {
			if(file2.isFile()){
				this.insertData(file2);
			}else if(file2.isDirectory()){
				String absolutePath = file2.getAbsolutePath();
				this.readFile(absolutePath);
			}
		}
	}
	
	
	public void inintTags(){
		String path = "D:\\Manipula\\Manipula\\pic\\p-clazz";
		List<Tags> geetTags = TagsDao.me.geetTags();
		File file = new File(path);
		File[] listFiles = file.listFiles();
		for (File file2 : listFiles) {
			if(file2.isDirectory()){
				int index = 0;
				for (int i = 0; i < geetTags.size(); i++) {
					Tags tags = geetTags.get(i);
					if(!file2.getName().equals(tags.getTagNo())){
						index ++;
					}else{
						break;
					}
				}
				if(index==geetTags.size()){
					
					System.out.println("add--------");
					
					Tags tempTag = new Tags();
					tempTag.setSerialId(GetWaterId.getWaterId());
					tempTag.setTagNo(file2.getName());
					tempTag.setTagName("-------");
					tempTag.setTypeNo("1");
					tempTag.setTypeName("gallery");
					tempTag.setUpdateUserNo("201711111723538841");
					tempTag.setUpdateTime(DateUtils.getCurrDateTime());
					TagsDao.me.insert(tempTag);
				}
			}
		}
		tags = TagsDao.me.geetTags();
	}
	
	public void insertData(File file ){
		
		
		
		
		String showName = file.getName();
		
		String absolutePath = file.getAbsolutePath();
		String[] split2 = absolutePath.split("\\\\");
		String string = split2[split2.length-2];
		
//		List<UserUploadInf> geetUserUploadInfList = UserUploadInfDao.me.geetUserUploadInfList(showName);
//		
//		if(geetUserUploadInfList!=null && geetUserUploadInfList.size()>0){
//			if(geetUserUploadInfList.size()>1){
//				System.out.println("fileName 重名："+showName);
//			}else{
//				//System.out.println("fileName 存在："+showName);
//				return;
//			}
//		}else{
			String[] split = showName.split("\\.");
			UserUploadInf userUploadInf = new UserUploadInf();
			String waterId = GetWaterId.getWaterId();
			userUploadInf.setSerialId(waterId);
			userUploadInf.setExpandName(split[1]);
			userUploadInf.setFileShowName(showName);
			userUploadInf.setFileName(showName);
			userUploadInf.setIsDelete("0");
			userUploadInf.setUploadUser("201711111723538841");
			userUploadInf.setUploadTime(DateUtils.getCurrDateTime());
			
			
			System.out.println(string);
			userUploadInf.setUploadPath("grab/pixiv"+"/"+string);
			userUploadInf.setTags(string+",");
			
			UserUploadInfDao.me.insert(userUploadInf);
			
			GalleryTags tag = new GalleryTags();
			tag.setSerialId(GetWaterId.getWaterId());
			
			String tagName = "";
			String tagNo ="" ;
			
			
			if(tags!=null){
				for (int i = 0; i < tags.size(); i++) {
					Tags tags2 = tags.get(i);
					if(tags2.getTagNo().equals(string)){
						
						tagName = tags2.getTagName();
						tagNo = string;
					}
				}
			}
			tag.setTagNames(tagName);
			tag.setTagNo(tagNo);
			tag.setIdDelete("0");
			tag.setUserUploadInfId(waterId);
			tag.setUpdateTime(DateUtils.getCurrDateTime());
			tag.setUpdateUser("201711111723538841");
			GalleryTagsDao.me.insert(tag);
			
//		}
		
		
	}

	
}
